/**
 * @author Vitor Franco do Carmo (vitor.carmo@capgemini.com)
 * @since 23/05/2014
 */
package br.com.vfcfw.controller.converter;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.FacesConverter;

import br.com.vfcfw.commons.util.StringUtil;

/**
 * Converter utilizado para todas as string que remove os espaços em branco antes e depois da mesma.
 * 
 * @author Vitor Franco do Carmo (vitor.carmo@capgemini.com)
 * @since 23/05/2014
 * 
 */
@FacesConverter(forClass = String.class)
public class StringTrimConverter extends BaseConverter {

    /**
     * {@inheritDoc}
     * 
     * @see javax.faces.convert.Converter#getAsObject(javax.faces.context.FacesContext,
     *      javax.faces.component.UIComponent, java.lang.String)
     */
    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String valor) {

        if (valor != null) {

            valor = StringUtil.removerEspacos(valor);
        }
        return valor;
    }

    /**
     * {@inheritDoc}
     * 
     * @see javax.faces.convert.Converter#getAsString(javax.faces.context.FacesContext,
     *      javax.faces.component.UIComponent, java.lang.Object)
     */
    @Override
    public String getAsString(FacesContext context, UIComponent component, Object valor) {

        return (String) valor;
    }

}
